package com.example.checkqufuface.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.R;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.View.QufuFaceProject.LoginActivity;
import com.example.checkqufuface.View.QufuFaceProject.QufuMainActivity;
import com.example.checkqufuface.bean.QufuLoginRequestBean;
import com.example.checkqufuface.bean.QufuLoginResultBean;

import static com.example.baselibrary.Utils.Customer.BASE_URL;

public class SelfToastUtil extends Toast {

    private static SelfToastUtil instance;

    private Toast mToast;
    private TextView mTextView;
    private String message;
    private Handler mHandler = new Handler();
    private boolean canceled = true;

    private QufuLoginRequestBean loginRequestBean;
    private String DeviceId;

    public static synchronized SelfToastUtil getInstance() {
        if (instance == null) {
            instance = new SelfToastUtil(MyApplication.getAppContext());
        }
        return instance;
    }

    public SelfToastUtil(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //自定义布局
        View view = inflater.inflate(R.layout.toast_self, null);
        //自定义toast文本
        mTextView = (TextView) view.findViewById(R.id.tv_toast);
        if (mToast == null) {
            mToast = new Toast(context);
        }
        //设置toast居中显示
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(view);
    }

    /**
     * 自定义居中显示toast
     */
    public void show(String s) {
//        if (mToast != null) {
//            mToast.cancel();
//            mToast = null;
//        }

        //如果提示---用户登录失败，请重新登入 那么就去掉登入接口
        if (s!=null && s.contains("请重新登")){
            goToLogin();
        }
        mTextView.setText(s);
        mToast.show();
    }

    private void goToLogin() {

        if (loginRequestBean == null) {
            loginRequestBean = new QufuLoginRequestBean();
        }

        String url =  (String) SPUtils.get(MyApplication.getAppContext(), Customer.QUFU_INPUT_URL, "");
        final String account =  (String) SPUtils.get(MyApplication.getAppContext(), Customer.QUFU_LOGIN_ACCOUNT, "");
        final String password =  (String) SPUtils.get(MyApplication.getAppContext(), Customer.QUFU_LOGIN_PASSWORD, "");
        if (StringUtil.isNullOrEmpty(account)) {
            return;
        }

        if (StringUtil.isNullOrEmpty(password)) {
            return;
        }
        loginRequestBean.setUserName(account);
        loginRequestBean.setPassword(password);
        loginRequestBean.setLoginType("3");
        loginRequestBean.setTerminalComputerCpuId(DeviceId);
        RetrofitClient.getInstance(MyApplication.getAppContext(), url).QuFulogin(loginRequestBean, new BaseSubscriber<QufuLoginResultBean>(MyApplication.getAppContext()) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);
            }

            @Override
            public void onNext(QufuLoginResultBean qufuLoginResultBean) {
                String code = qufuLoginResultBean.getCode();
                if (code.equals("200")) {
                        SPUtils.put(MyApplication.getAppContext(), Customer.QUFU_LOGIN_ACCOUNT, account);
                        SPUtils.put(MyApplication.getAppContext(), Customer.QUFU_LOGIN_PASSWORD, password);
                        SPUtils.put(MyApplication.getAppContext(), Customer.QUFU_IS_REMBER, true);

                    SPUtils.put(MyApplication.getAppContext(), Customer.QUFU_ACCESSTOKEN, qufuLoginResultBean.getData().getUserInfo().getAccessToken());
                    SPUtils.put(MyApplication.getAppContext(), Customer.QUFU_REFRESHTOKEN, qufuLoginResultBean.getData().getUserInfo().getRefreshToken());
                    MyApplication.getAppContext().startActivity(new Intent(MyApplication.getAppContext(), QufuMainActivity.class));
                } else {
                    SelfToastUtil.getInstance().show(qufuLoginResultBean.getMessage());
                }
            }
        });
    }

}